<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Student as StudentModel;
use think\Request;
use think\Db;

class Student extends BaseController
{
    public function list($page = 1,$q='')
    {
        //$student = new StudentModel;
        $q = input('get.q');
        $pageParam = ['query' => []];
        //$list = StudentModel::order('stu_id desc')->paginate(15);
        if($q){
            $pageParam['query']['q'] = $q;
            $list = StudentModel::where('stu_no|stu_name','LIKE',"%$q%")->order('stu_id desc')->paginate(15,false,$pageParam);            
        }else{
            $list = StudentModel::order('stu_id desc')->paginate(15);
        }
        
        $studentCount = StudentModel::count();

        $this->assign('studentCount',$studentCount);
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('q',$q);
        return $this->fetch();
    }

    public function add()
    {
        if(request()->isPost())
        {
            $student = new StudentModel;
            if ($student->allowField(true)->validate(true)->save(input('post.'))) {
                //return '用户[ ' . $student->stu_name . ':' . $student->stu_id . ' ]新增成功';
                return redirect('student/list');
            } else {
                //return $student->getError();
                $this->error($student->getError());
            }
            //return redirect('student/list');
        }
        return $this->fetch();
    }

    public function edit($stu_id = 0,$page = 1,$q='')
    {
        $student = StudentModel::get($stu_id);
        if($student)
        {
            $this->assign('student',$student);
        } else {
            echo '非法参数！';die;
        }

        if(request()->isPost())
        {
            if ($student->allowField(true)->validate(true)->save(input('post.'))) {
                //return '用户[ ' . $student->stu_name . ':' . $student->stu_id . ' ]更新成功';
                return redirect('student/list',['page' => $page,'q' => $q]);
                //$this->success('更新成功');
            } else {
                //return $student->getError();
                $this->error($student->getError());
            }
        }
        $this->assign('page',$page);
        //$this->assign('q',$q);
        return $this->fetch();
    }

    // 删除学生数据
    public function del($stu_id = 0)
    {
        $student = StudentModel::get($stu_id);
        if($student)
        {
            $this->assign('student',$student);
        } else {
            echo '非法参数！';die;
        }

        if(Request()->isPost())
        {
            $result = StudentModel::destroy($stu_id);
            if ($result) {
                return redirect('student/list');
            } else {
                $this->error('删除用户错误！');
            }
        }

        return $this->fetch();
        
    }

    //清空数据
    public function empty()
    {
        if(Request()->isPost())
        {
            // 清空数据表
            $result = Db::execute('TRUNCATE table xk_student');
            if ($result===0) {
                return redirect('student/list');
            } else {
                $this->error('操作错误！');
            }
        }

        return $this->fetch();
        
    }

    public function import(Request $request)
    {
        if(request()->isPost())
        {
           // 获取表单上传文件
            $file = $request->file('excelTable');
            // 上传文件验证
            $result = $this->validate(['file' => $file], ['file'=>'require|fileExt:xls,xlsx'],['file.require' => '请选择上传文件', 'file.fileExt' => '非法excel文件']);
            if(true !== $result){
                $this->error($result);
            }
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'excel');
            if ($info) {
                //$this->success('文件上传成功：' . $info->getRealPath());die;
                //读取excel数据写入数据库
                $data = importExcel($info->getRealPath(),$info->getExtension());
                //dump($data);die;
                foreach ($data as $key => $val) {
                    if ($key > 1) {
                        $datas['school_id'] = $val['A'];
                        $datas['grade'] = $val['B'];
                        $datas['class'] = $val['C'];
                        $datas['stu_no'] = strtolower($val['D']);
                        $datas['stu_name'] = $val['E'];
                        $datas['gender'] = $val['F'];
                        $datas['stu_password'] = strtolower($val['G']);
                        
                        //性别转换
                        $datas['gender'] = $datas['gender'] == '男' ? 0:1;
                        $newData[] = $datas;
                    }
                }
                $student = new StudentModel;
                $result = $student->saveAll($newData);
                //dump($result);die;
                return redirect('student/list');
            } else {
                // 上传失败获取错误信息
                $this->error($file->getError());
            } 
        }

        return $this->fetch();
    }
}